/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice;

import org.proteusframework.core.api.model.INamespace;

final class ScopedServiceInstance<T extends IServicePlugin>
{
    private final Class<T> servicePluginInterface;
    private final ServiceScope serviceScope;
    private final INamespace namespace;
    private final T instance;

    ScopedServiceInstance(Class<T> servicePluginInterface, ServiceScope serviceScope, T instance)
    {
        this(servicePluginInterface, serviceScope, null, instance);
    }

    ScopedServiceInstance(Class<T> servicePluginInterface, ServiceScope serviceScope, INamespace namespace, T instance)
    {
        this.servicePluginInterface = servicePluginInterface;
        this.serviceScope = serviceScope;
        this.namespace = namespace;
        this.instance = instance;
    }

    public Class<T> getServicePluginInterface()
    {
        return servicePluginInterface;
    }

    public ServiceScope getServiceScope()
    {
        return serviceScope;
    }

    public INamespace getNamespace()
    {
        return namespace;
    }

    public T getInstance()
    {
        return instance;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        ScopedServiceInstance that = (ScopedServiceInstance) o;

        if (namespace != null ? !namespace.equals(that.namespace) : that.namespace != null)
        {
            return false;
        }
        if (!servicePluginInterface.equals(that.servicePluginInterface))
        {
            return false;
        }
        if (serviceScope != that.serviceScope)
        {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = servicePluginInterface.hashCode();
        result = 31 * result + serviceScope.hashCode();
        result = 31 * result + (namespace != null ? namespace.hashCode() : 0);
        return result;
    }
}
